﻿using UnityEngine;
using System.Collections;
using  System.IO;

public class ConfigModelGen  {



    private  static readonly string sourcePath = Application.streamingAssetsPath + "/Data/";
    private static readonly string modelPath = Application.dataPath + "/Playing/Scripts/Data/";


    /// <summary>
    /// 生成配置文件实体模型文件
    /// </summary>
    public static  void Gen()
    {
        EmptyFolder(modelPath);
        string[] files = Directory.GetFiles(sourcePath, "*.txt");
        Debug.Log("开始生成实体文件...");
        foreach (string file in files)
        {
            string filePath = file;
            string fileName = Path.GetFileNameWithoutExtension(filePath);
            if (fileName=="version") continue;
            fileName = FirstToUpper(fileName);
            
            using (FileStream fs = new FileStream(modelPath + fileName+".cs",FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    ArrayList textData = FileHelper.LoadFile(file);
                    //注释
                    string[] comments = textData[0].ToString().Split('\t');
                    //属性
                    string[] propertys = textData[1].ToString().Split('\t');
                    //第一行数据。用于推断属性的类型
                    string[] values01 = textData[2].ToString().Split('\t');

                    //引入System命名空间。
                    sw.WriteLine("using System;");

                    //开始拼接类
                    //类说明
                    sw.WriteLine("    /// <summary>\n" + "    /// " + fileName + "\n    /// </summary>");

                    //标记为可序列化
                    sw.WriteLine("[Serializable]");
                    //写类的定义
                    sw.WriteLine(" public class " + fileName);
                    sw.WriteLine("    {");
                    //写属性
                    for (int f = 0; f < propertys.Length; f++)
                    {
                        string pp = propertys[f].Trim();
                        sw.WriteLine("       private " + GetTypeByString(values01[f].Trim()).ToString() + " " + pp + ";\n" + "       /// <summary>\n" + "       /// " + comments[f].Trim() + "\n" +
                                     "       /// </summary>\n" +
                                     "       public" + " " + GetTypeByString(values01[f].Trim()).ToString() + " " + FirstToUpper(pp) + "\n" +
                                     "       {\n" +
                                     "         get { return " + pp + "; }\n" +
                                     "         set { " + pp + " = value; }\n" +
                                     "       }\n");
                    }
                    sw.Write("    }");
                } 
            }
        }
        Debug.Log("生成成功...");
    }


    /// <summary>
    /// 清空文件夹类容，但不删除文件夹
    /// </summary>
    /// <param name="dir"></param>
    private static void EmptyFolder(string dir)
    {
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        foreach (var d in Directory.GetFileSystemEntries(dir))
        {
            if (File.Exists(d))
            {
                File.Delete(d);
            }
            if (Directory.Exists(d))
            {
                DeleteFolderForce(d);
            }

        }
    }

    /// <summary>
    /// 强制删除文件夹
    /// </summary>
    /// <param name="dir"></param>
    private static void DeleteFolderForce(string dir)
    {
        if (Directory.Exists(dir))
        {
            DirectoryInfo di = new DirectoryInfo(dir);
            di.Delete(true);
        }
    }
    /// <summary>
    /// 用来将首字母大写，如果以下划线开头，则把第二个字母大写
    /// </summary>
    /// <param name="letter">传入的字符串</param>
    /// <returns></returns>
    private static string FirstToUpper(string letter)
    {
        letter = letter.Trim();
        string str;
        //这种情况是为Java的命名规范设计的
        if (letter.Substring(0, 1) == "_")
        {
            str = letter.Substring(1, 1);
            str = str.ToUpper();
            return str + letter.Substring(2);
        }

        str = letter.Substring(0, 1);
        str = str.ToUpper();
        return str + letter.Substring(1);
    }

    //根据传入类型生成相应的字段类型。  
    private static string GetTypeByString(string letter)
    {
        string str = "string";
        switch (letter.Trim())
        {

            case "i":
                str = "int";
                break;
            case "f":
                str = "float";
                break;
            case "s":
                str = "string";
                break;
            default:

                Debug.LogError("请配置正确的类型:" + letter.Trim());
                break;
        }
        return str;
    }

}
